package com.wanxi.util;

import java.util.Arrays;
import java.util.Random;

public class Score {
    public static void main(String[] args) {
        /**
         * 1.创建一个数组，里面是分数，可能有小数,不能太低 ，有一个最低分80，最高分100
         * 2.求最小值
         * 3.求最大值
         * 4.求和
         * 5.求平均分
         */
        double[] arr = getScore(80.0, 100.0, 10);
        System.out.println(Arrays.toString(arr));
        double min = getMin(arr);
        double max = getMax(arr);
        double sum = getSum(arr);
        double avg = (sum - min - max) / (arr.length - 2);
        System.out.println("最低分：" + min);
        System.out.println("最高分：" + max);
        System.out.println("总分：" + sum);
        System.out.println("平均分：" + avg);
//        80-100之间的整数  nextInt(end-start)+start;
//        80-100之间的小数
//        while (true) {
//            int aa = (int) (new Random().nextDouble() * 2100);
//            double bb = aa / (100 * 1.0) + 80;
//            System.out.println(bb);
//        }
    }

    private static double getSum(double[] arr) {
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }

    private static double getMax(double[] arr) {
        double max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
        }
        return max;
    }

    private static double getMin(double[] arr) {
        double min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];
            }
        }
        return min;
    }

    private static double[] getScore(double start, double end, int length) {

        double[] arr = new double[length];

        for (int i = 0; i < length; i++) {
            int aa = (int) (new Random().nextDouble() * ((end - start) * 100));
            double bb = aa / (100 * 1.0) + 80;
            String str = bb + "";//将bb转换为字符串
//           取得字符串里面的前五个字符，然后转换为double
            if (str.length() > 5) {
                arr[i] = Double.parseDouble(str.substring(0, 5));
            }else {
                arr[i] = bb;
            }
        }
        return arr;
    }

    /**
     * 对数组进行赋初始值
     *
     * @param length
     */
    private static double[] setArrData(int length) {
        double[] arr = new double[length];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = new Random().nextDouble();
        }
        return arr;
    }
}
